<template>
  <view class="register-page">
    <view class="header">
      <button class="back-btn" @click="navigateBack">
        <text class="icon">←</text> 返回登录
      </button>
      <text class="title">注册账号</text>
    </view>
    
    <view class="form-container">
      <view class="form-item">
        <text class="label">用户名</text>
        <input 
          class="input" 
          placeholder="请输入用户名 (2-8字符)"
          v-model="username"
          @input="clearError"
        />
      </view>
      
      <view class="form-item">
        <text class="label">密码</text>
        <input 
          class="input" 
          type="password"
          placeholder="请输入密码 (6-16字符)"
          v-model="password"
          @input="clearError"
        />
      </view>
      
      <view class="form-item">
        <text class="label">确认密码</text>
        <input 
          class="input" 
          type="password"
          placeholder="请再次输入密码"
          v-model="confirmPassword"
          @input="clearError"
        />
      </view>
      
      <text class="error-message" v-if="errorMessage">{{ errorMessage }}</text>
      
      <button class="register-btn" @click="handleRegister">
        注册
      </button>
    </view>
  </view>
</template>

<script setup>
import { ref } from 'vue'
import { useUserStore } from '@/store/user'

const userStore = useUserStore()
const username = ref('')
const password = ref('')
const confirmPassword = ref('')
const errorMessage = ref('')

const clearError = () => {
  errorMessage.value = ''
}

const navigateBack = () => {
  uni.navigateBack()
}

const handleRegister = async () => {
  const user = username.value.trim()
  const pwd = password.value.trim()
  const confirmPwd = confirmPassword.value.trim()
  
  // 验证表单
  if (user.length < 2 || user.length > 8) {
    errorMessage.value = '用户名长度必须为2-8个字符'
    return
  }
  
  if (pwd.length < 6 || pwd.length > 16) {
    errorMessage.value = '密码长度必须为6-16个字符'
    return
  }
  
  if (pwd !== confirmPwd) {
    errorMessage.value = '两次输入的密码不一致'
    return
  }
  
  // 调用注册接口
  const result = await userStore.register(user, pwd)
  if (result.success) {
    uni.showToast({
      title: '注册成功，请登录',
      icon: 'none',
      duration: 2000
    })
    setTimeout(() => {
      navigateBack()
    }, 2000)
  } else {
    errorMessage.value = result.message
  }
}
</script>

<style scoped>
.register-page {
  background-color: #1a1a2e;
  min-height: 100vh;
  padding: 48rpx 24rpx;
}

.header {
  display: flex;
  align-items: center;
  margin-bottom: 48rpx;
}

.back-btn {
  background-color: transparent;
  color: #ccc;
  padding: 8rpx 16rpx;
  font-size: 28rpx;
  display: flex;
  align-items: center;
  margin-right: 24rpx;
}

.icon {
  margin-right: 8rpx;
}

.title {
  font-size: 36rpx;
  font-weight: bold;
  color: #fff;
  flex: 1;
}

.form-container {
  width: 100%;
  max-width: 500rpx;
  margin: 0 auto;
}

.form-item {
  margin-bottom: 24rpx;
}

.label {
  display: block;
  font-size: 28rpx;
  color: #ccc;
  margin-bottom: 8rpx;
}

.input {
  width: 100%;
  padding: 20rpx;
  background-color: #2d3436;
  border: 1px solid #444;
  border-radius: 8rpx;
  color: #fff;
  font-size: 28rpx;
}

.error-message {
  color: #e74c3c;
  font-size: 24rpx;
  margin-bottom: 16rpx;
  display: block;
  height: 24rpx;
}

.register-btn {
  width: 100%;
  background-color: #6C5CE7;
  color: #fff;
  padding: 20rpx;
  border-radius: 8rpx;
  font-size: 32rpx;
}
</style>